package com.chinatelecom.clientservice.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.chinatelecom.clientservice.pojo.ExtGridReturn;
import com.chinatelecom.clientservice.pojo.ExtPager;
import com.chinatelecom.clientservice.pojo.ExtReturn;
import com.chinatelecom.clientservice.pojo.client.PayScens;
import com.chinatelecom.clientservice.service.client.PaymentService;

@Controller
@RequestMapping("/payment")
public class PaymentController {
	@Autowired
	private PaymentService paymentService;

	@RequestMapping(value = "/payScens/scenarios")
	public String scenariosInit() {
		String abc = null ;
		System.out.println(abc.indexOf(4));
		return "payment/scenarios";
	}

	@RequestMapping(value = "/payScens/searchAll")
	@ResponseBody
	public ExtGridReturn searchAll(ExtPager pager) {
		ExtGridReturn results = paymentService.payScensList(null, pager);
		return results;
	}

	@RequestMapping(value = "/payScens/search/{scenId}")
	@ResponseBody
	public Map<String, Object> searchScenariosInfo(@PathVariable String scenId) {
		Map<String, Object> results = new HashMap<String, Object>();
		results.put("success", true);
		results.put("data", paymentService.searchPaySences(scenId));
		return results;
	}

	@RequestMapping(value = "/payScens/savePayScens")
	@ResponseBody
	public ExtReturn saveScenariosInfo(PayScens payScens) {
		int results;
		try {
			results = paymentService.savePaySencens(payScens);
			if (results == 0) {
				return new ExtReturn(false, "保存失败!");
			} else if (results == 0) {
				return new ExtReturn(true, "保存成功 !");
			}
		} catch (Exception e) {
			return new ExtReturn(false, e.getMessage());
		}

		return new ExtReturn(false, "保存失败!");
	}

	/************************ 支付入口 ****************************/
	@RequestMapping(value = "payEntry/all")
	public String payEntryAll() {
		return "payment/entrys";
	}

	@RequestMapping(value = "/payEntry/searchAll")
	@ResponseBody
	public ExtGridReturn searchAllOfPayEntry(ExtPager pager) {
		ExtGridReturn results = paymentService.payEntryList(null, pager);
		return results;
	}

	@RequestMapping(value = "/payEntry/search/{entryId}")
	@ResponseBody
	public Map<String, Object> searchEntryInfo(@PathVariable String entryId) {
		Map<String, Object> results = new HashMap<String, Object>();
		results.put("success", true);
		results.put("data", paymentService.searchPayEntry(entryId));
		return results;
	}

	/************************ 支付方式 ****************************/
	@RequestMapping(value = "/payMode/all")
	public String payModeAll() {
		return "payment/modes";
	}

	@RequestMapping(value = "/payMode/searchAll")
	@ResponseBody
	public ExtGridReturn searchAllOfPayMode(ExtPager pager) {
		ExtGridReturn results = paymentService.payModeList(null, pager);
		return results;
	}

	@RequestMapping(value = "/payMode/search/{modeId}")
	@ResponseBody
	public Map<String, Object> searchModeInfo(@PathVariable String modeId) {
		Map<String, Object> results = new HashMap<String, Object>();
		results.put("success", true);
		results.put("data", paymentService.searchPayMode(modeId));
		return results;
	}
	
	@RequestMapping(value = "/payMode/searchAllOfHelp")
	@ResponseBody
	public ExtGridReturn searchAllOfHelpOfPayMode(ExtPager pager,@RequestParam(value="orderType",required = false)String orderType,@RequestParam(value="payScene",required = false)String payScene,@RequestParam(value="amount",required = false)String amount,@RequestParam(value="version",required = false)String version) {
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("orderType", orderType);
		param.put("payScene",payScene);
		param.put("amount",Float.parseFloat(amount));
		param.put("version", Integer.parseInt(version));
		paymentService.payModeListOfHelp(param, pager);
		return new ExtGridReturn(0, (List)param.get("V_RESULT"));
	} 

}
